<template>
	<view class="work-container">
		<u-navbar title="工作台" :autoBack="true" leftText='返回' :placeholder="true">
			<view class="u-nav-slot" slot="left"></view>
			<view class="u-nav-slot" slot="right">
				<view class="tenant-box">
					<view @click="showTenantSwitch=true" style="display: flex;">
						<view style="width: 120px;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;text-align: right;">{{activeTenantData.name||'请选择资产单位'}}</view>
						<image src="@/static/images/switch.png" style="width: 14px;height: 14px;margin-top: 4px;"></image></view>
				</view>
			</view>
		</u-navbar>

		<!-- 轮播图 -->
		<uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
			<swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
				<swiper-item v-for="(item, index) in data" :key="index">
					<view class="swiper-item" @click="clickBannerItem(item)">
						<image :src="item.image" mode="aspectFill" :draggable="false" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>
		<!-- 宫格组件 -->
		<!-- <uni-section title="系统管理" type="line"></uni-section> -->
		<view class="grid-body" style="background-color: #fff;">
			<uni-grid :column="4" :showBorder="false">
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid1">
						<uni-icons type="settings-filled" size="30"></uni-icons>
						<text class="text">资产列表</text>
					</view>
				</uni-grid-item	>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid2">
						<uni-icons type="person-filled" size="30"></uni-icons>
						<text class="text">资产盘点</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid">
						<uni-icons type="person-filled" size="30"></uni-icons>
						<text class="text">用户管理</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid">
						<uni-icons type="staff-filled" size="30"></uni-icons>
						<text class="text">角色管理</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid">
						<uni-icons type="color" size="30"></uni-icons>
						<text class="text">菜单管理</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid">
						<uni-icons type="settings-filled" size="30"></uni-icons>
						<text class="text">部门管理</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid">
						<uni-icons type="heart-filled" size="30"></uni-icons>
						<text class="text">岗位管理</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid">
						<uni-icons type="bars" size="30"></uni-icons>
						<text class="text">字典管理</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid5">
						<uni-icons type="gear-filled" size="30"></uni-icons>
						<text class="text">参数设置</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid">
						<uni-icons type="chat-filled" size="30"></uni-icons>
						<text class="text">通知公告</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="changeGrid">
						<uni-icons type="wallet-filled" size="30"></uni-icons>
						<text class="text">日志管理</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>

	
		<u-popup :show="showTenantSwitch" mode="right" closeIconPos='top-right' @close="showTenantSwitch=false" style="width: 200px">
			<view>
				<u-divider text="资产单位切换"></u-divider>
				<view v-for="(item,index) in tenantList" :key="index" class="tenant-item" @click="handleTenant(item)" :class="activeTenantData.id==item.id?'active':''">
					{{item.name}}
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getTenantSimpleList
	} from '@/api/tenant/index.js'
	import {getUserPermission} from '@/api/system/user.js'
	export default {
		data() {
			return {
				current: 0,
				showTenantSwitch: false,
				swiperDotIndex: 0,
				tenantList: [],
				activeTenantData: uni.getStorageSync("active_tenant"),
				show: false,
				data: [{
					image: '/static/images/banner/banner01.png'
				}]
			}
		},
		mounted() {
			this.getUserPermission()
			this.getTenantSimpleList()
		},
		methods: {
			async getUserPermission(){
				await getUserPermission()
			},
			handleTenant(item){
				uni.setStorageSync("active_tenant",item)
				this.activeTenantData = item
				this.showTenantSwitch = false
				uni.showToast({
					icon:'none',
					title:"资产单位切换成功!",
				})
			},
			async getTenantSimpleList() {
				const ret = await getTenantSimpleList()
				this.tenantList = ret.data || []
				const active_tenant = uni.getStorageSync("active_tenant")
				if(!active_tenant && this.tenantList.length) {
					this.activeTenantData = this.tenantList[0]
					uni.setStorageSync("active_tenant",this.tenantList[0])
				}else if(active_tenant && this.tenantList.length){
					const one = this.tenantList.find(v=>v.id==active_tenant.id)
					if(!one) {
						this.activeTenantData = this.tenantList[0]
						uni.setStorageSync("active_tenant",this.tenantList[0])
					}
				}
			},
			clickBannerItem(item) {
				console.info(item)
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			changeGrid2(e) {
				const tenant = uni.getStorageSync("active_tenant")
				if(!tenant){
					return this.$modal.showToast('请选择资产单位')
				}
				uni.removeStorageSync("edit_barcodeNumber")
				uni.removeStorageSync("edit_copy")
				this.$tab.navigateTo('/pages/asset/asset')
			},
			changeGrid(e) {
				this.$modal.showToast('模块建设中~')
			},
			changeGrid1(e) {
				this.$tab.navigateTo('/pages/asset/list')
			},
			changeGrid5(e) {
				this.$tab.navigateTo('/pages/params-setting/index')
			},
			handleTenantSelect(e) {
				const {
					label,
					value
				} = e[0]
				this.activeTenantData = {
					name: label,
					id: value,
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f7f7f7;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	/* #endif */

	.text {
		text-align: center;
		font-size: 26rpx;
		margin-top: 10rpx;
	}
	
	.tenant-item{
		white-space: nowrap;
		border-bottom: 1px solid #eee;
		padding: 10px;
	}
	
	::v-deep .u-popup__content{
		width: 260px;
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
	}

	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 150px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}

	.tenant-box {
		padding: 0 0 0 10px;
	}
	.active{
		color: dodgerblue;
	}
</style>